/**
 * 
 */
package com.jie.eclipse.pan.gef.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;

import com.jie.eclipse.pan.bind.BaseDataBind;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.editors.utils.PanImageHelper;
import com.jie.eclipse.pan.form.support.FormItem;
import com.jie.eclipse.pan.utils.ObjectHelper;
import com.jie.eclipse.pan.utils.StringBuilderEx;
import com.jie.eclipse.pan.utils.StringHelper;

/**
 * @author yanjie
 *
 */
public class BaseDialog extends Dialog{
	private BaseDataEntity data = null;//控件的基本信息
	private BaseDataEntity defaultData = null;//对话框默认数据
	private BaseDataEntity tempDate = null;//对话框默认数据
	private FormItem root = null;
	private String action = "";

	public FormItem getRoot() {
		return root;
	}

	public void setRoot(FormItem root) {
		this.root = root;
	}

	private BaseDataBind dataBind = null;//数据绑定
	
	public void reloadRecord(){
		if(this.getDefaultData() != null)
			this.dataBind.setRecord(this.getDefaultData());
	}
	
	public void initDefaultData(){
		
	}
	
	//設置默認值
	public BaseDataEntity getDefaultData(){
		return defaultData;
	}
	
	public BaseDialog(Shell parentShell,BaseDataEntity data,String action){
		super(parentShell);
		this.dataBind = new BaseDataBind();
		this.data = data;
		this.action = action;
		initDefaultData();
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(this.getTitle());
		newShell.setImage(PanImageHelper.getInstance().getImage("icons/ext/fam/user_green.gif"));
	}
	
	@Override
	protected int getShellStyle() {
		return super.getShellStyle() | SWT.RESIZE | SWT.MAX | SWT.MIN | SWT.SCROLL_PAGE;
	}
	
	@Override
	protected void okPressed() {
		if("fillConfig".equalsIgnoreCase(action)){
			//from basepanelDialog to fill component config
			fillConfig(data,dataBind);
		}else if("fillPanConfig".equalsIgnoreCase(action)){
			//配置小助手
			fillPanConfig(data,dataBind);
		}else{
			//return component's html code
			this.data.spsv("code", getHtmlCode(data,dataBind));
		}
		super.okPressed();
	}
	
	public void initRootItem(BaseDataEntity data,BaseDataBind dataBind,StringBuilderEx code){
		
	}
	
	public void fillConfig(BaseDataEntity config,BaseDataBind dataBind){
		if(config ==null || dataBind==null)return;
		BaseDataEntity record = dataBind.getRecord();
		config.spsv("name", record.gpsv("name"));
		config.spsv("caption", record.gpsv("caption"));
		if(record.gpbv("required", false)){
			config.spsv("required","true");
			config.spsv("required_text","N");
		}else{
			config.spsv("required","false");
			config.spsv("required_text","Y");
		}
	}
	
	public void fillPanConfig(BaseDataEntity config,BaseDataBind dataBind){
		
	}
	
	public BaseDataEntity getData() {
		return data;
	}
	
	public BaseDataBind getDataBind() {
		return dataBind;
	}
	
	private String getHtmlCode(BaseDataEntity data,BaseDataBind dataBind){
		StringBuilderEx code = new StringBuilderEx();
		initRootItem(data,dataBind,code);
		if(this.getRoot() != null){
			this.getRoot().onRender(code);
		}
		return code.toString();
	}
	
	public void setDefaultData(BaseDataEntity defaultData) {
		this.defaultData = defaultData;
	}
	
	public String getTitle(){
		return "";
	}
	
	@Override
	protected Point getInitialSize() {
		Point point = super.getInitialSize();
		return point;
	}
	
	public BaseDataEntity getDataInstance(){
		if(this.tempDate == null){
			this.tempDate = new BaseDataEntity();
		}
		return this.tempDate;
	}
	
	public FormItem getNewFormItem(){
		String xtype = this.getClass().getName();
		String strClass = QMDao.getInstance().getClassDFMap().get(xtype);
		FormItem formItem = null;
		if(!StringHelper.IsNullOrEmpty(strClass)){
			formItem = (FormItem)ObjectHelper.newInstance(strClass,null);
		}
		return formItem;
	}
	
	public QMDao getQmDao(){
		return QMDao.getInstance();
	}
}
